package cate.game.role.bag.prop.msg;

import cate.common.annotation.OperationSetter;
import cate.common.annotation.SplitParse;
import cate.common.table.d.*;
import cate.common.table.item.EquipBoxRow;
import cate.common.table.item.equip.row.EquipBaseRow;
import cate.common.util.SplitParser;
import cate.common.util.XT;
import cate.common.util.random.RWItem;
import cate.common.util.random.RWList;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MergeMsgSender;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBBag;
import cate.game.notice.msg.NoticeDialogResp;
import cate.game.res.MixRes;
import cate.game.res.RewardShow;
import cate.game.role.Role;
import cate.game.role.bag.equip.Equip;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@ClientMsg(title = "装备宝箱自选 の 请求")
@OperationSetter(value = GDOperation.EQUIP_BOX_USE, sub = "tid")
public class EquipBoxUseReq implements MsgInput {

    @NoteField(value = "道具tid")
    public int tid;

    @NoteField("职业")
    public byte job;

    @NoteField("部位")
    public byte pos;

    @NoteField("阵营印记")
    public byte campMark;


    @Override
    public void execute(GameBody game, ClientContext cc) {
        EcResult r = settle(cc.getRole());
        game.notice.message(cc.getRole(), r, true);
    }

    private EcResult settle(Role role) {
        EcResult r = new EcResult();
        EquipBoxRow boxRow = role.getGame().table.item.equipBox.get(tid);
        if (boxRow == null) {
            return r.fail("配置不存在");
        }
        if (!GDHero.Job.valid(job)){
            return r.fail("职业有误");
        }
        r = role.getBag().prop.removeItem(tid, 1, null);
        if (!r.ok()) {
            return r;
        }
        List<Equip> equips = new ArrayList<>();
        switch (boxRow.type) {
            case ONE_POS:
                handleOnePos(role,equips, boxRow, job, pos);
                break;
            case ALL_POS:
                handleAllPos(role,equips, boxRow, job);
                break;
            case RANDOM_POS:
                handleRandom(role,equips, boxRow, job);
                break;
            default:
                break;
        }

        //重写阵营印记
        byte rewriteCampMark = -1;

        if (!XT.isBlank(boxRow.randomCampMarks)) {
            int index = XT.randomIndex(boxRow.randomCampMarks.size());
            rewriteCampMark = boxRow.randomCampMarks.get(index);
        } else {
            if (!XT.isBlank(boxRow.pickCampMarks) && boxRow.pickCampMarks.contains(campMark)) {
                rewriteCampMark = campMark;
            }
        }
        if (GDCamp.Type.valid(rewriteCampMark,false) || rewriteCampMark == GDCamp.Type.NULL) {
            for (Equip equip : equips) {
                equip.campMark = rewriteCampMark;
            }
        }
        MergeMsgSender sender = new MergeMsgSender();
        RewardShow show = new RewardShow();
        MixRes instanceReward = null;
        for (Equip equip : equips) {
            instanceReward = new MixRes(equip);
            instanceReward.add(role, true, GDOperation.EQUIP_BOX_USE, sender);
            show.addInsRwd(GDObj.Type.EQUIP, equip.uid);
        }
        sender.send(role, true);
        role.getGame().notice.dialog(role, NoticeDialogResp.TYPE_REWARD, "恭喜获得", 0, null, show.insRwd);
        return r;
    }

    private void handleOnePos(Role role, List<Equip> equips, EquipBoxRow boxRow, byte job, byte pos) {
        if (GDEquip.checkPos(boxRow.pos)) {
            pos = boxRow.pos;
        }
        EquipBaseRow equipRow = role.getGame().table.equip.base.findOne(job, pos, boxRow.quality);
        if (equipRow != null) {
            equips.add(new Equip(role, equipRow.id, 1));
        }
    }

    private void handleAllPos(Role role, List<Equip> equips, EquipBoxRow boxRow, byte job) {
        List<EquipBaseRow> equipRowList = role.getGame().table.equip.base.find(job, boxRow.quality);
        for (EquipBaseRow equipRow : equipRowList) {
            equips.add(new Equip(role, equipRow.id, 1));
        }
    }

    private void handleRandom(Role role, List<Equip> equips, EquipBoxRow boxRow, byte job) {
        byte randomPos = GDEquip.POS_1;
        if (StringUtils.isNotBlank(boxRow.exConfig)) {
            RWList<PosRandomConfig> rwList = new RWList<>();
            rwList.setup(SplitParser.parse(PosRandomConfig.class, boxRow.exConfig));
            randomPos = rwList.get().pos;
        }
        EquipBaseRow equipRow = role.getGame().table.equip.base.findOne(job, randomPos, boxRow.quality);
        if (equipRow != null) {
            equips.add(new Equip(role, equipRow.id, 1));
        }
    }


    @NoteClass("部位随机配置")
    public static class PosRandomConfig implements RWItem {

        @SplitParse(order = 0, defaultValue = "1", note = "位置")
        public byte pos;

        @SplitParse(order = 1, defaultValue = "1", note = "权重")
        public double weight;

        @Override
        public double getWeight() {
            return weight;
        }
    }



    @NoteField("选定一个部位")
    private static final byte ONE_POS = 1;
    @NoteField("所有部位")
    private static final byte ALL_POS = 2;
    @NoteField("随机一个部位")
    private static final byte RANDOM_POS = 3;





    @Override
    public MsgConfig conf() {
        return MsgIdBBag.EQUIP_BOX_USE_REQ;
    }

}
